Find the Frequency of Characters in a String using C++

04-11-17 Course- CPP

This program asks user to enter a string and a character and this program checks how many times that character is repeated in the string entered by user.

Source Code to Find the Frequency of Characters


#include <iostream>
#include <cstring>
using namespace std;
int main(){
   char c[1000],ch;
   int i,count=0;
   cout << "Enter a string: ";
   cin.getline(c, 1000);
   cout << "Enter a character to find frequency: ";
   cin >> ch;
   for(i=0;c[i]!='\0';++i)
   {
       if(ch==c[i])
           ++count;
   }
   cout << "Frequency of " << ch <<  " = " << count;
   return 0;
}

Output


Enter a string: i like programming
Enter a character to find frequency: i
Frequency of e = 3